<template>
  <div class="app-main-content">
    <el-button size="small" type="primary" @click="addOrUpdateHandle()">
      添加
    </el-button>
    <el-table
      v-loading="dataListLoading"
      :data="dataList"
      :height="tableHeight"
      border
      style="width: 100%;">
      <table-tree-column prop="name" label="部门" header-align="center"/>
      <el-table-column prop="parentName" label="上级部门" header-align="center" align="center"/>
      <el-table-column prop="sort" label="排序" header-align="center" align="center" width="80"/>
      <el-table-column :label="'操作'" fixed="right" header-align="center" align="center" width="150">
        <template slot-scope="scope">
          <el-button v-if="hasPermission('sys:dept:update')" type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button v-if="hasPermission('sys:dept:delete')" type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>

    </el-table>

    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"/>
  </div>
</template>

<script>
import * as API from '@/api/sys/dept'
import AddOrUpdate from './dept-add-or-update'
import * as Tool from '@/utils/tool'
import TableTreeColumn from '@/components/table-tree-column'
export default {
  components: {
    AddOrUpdate,
    TableTreeColumn
  },
  data() {
    return {
      tableHeight: Tool.getClientHeight() - 180,
      dataList: [],
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    }
  },
  mounted: function() {
    window.addEventListener('resize', this.setTableHeight)
    this.setTableHeight()
    this.getDataList()
  },
  methods: {
    setTableHeight() {
      this.$nextTick(() => {
        this.tableHeight = Tool.getClientHeight() - 155
      })
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      const params = {}
      API.list(params).then(response => {
        if (response && response.code === 0) {
          this.dataList = response.data
        } else {
          this.dataList = []
        }
        this.dataListLoading = false
      })
    },

    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    deleteHandle(id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`确定进行删除操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText:
            '取消',
        type:
            'warning'
      }).then(() => {
        API.del(ids).then(data => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    }
  }
}
</script>
